import axios from 'axios'
import { Message } from 'element-ui';
import i18n from '@/langs'

const msgList = [
  {'ErrorCode': 601, label: '账号信息不存在',message: i18n.t('DEFAULT.MSGACCOUNTNOTEXIT') },
  {'ErrorCode': 602, label: 'Password Error！',message:  i18n.t('DEFAULT.MSGPSWWRONG')},
  {'ErrorCode': 603, label: '没有用户登录信息',message: 'No user login information' },
  {'ErrorCode': 604, label: '账号信息已存在，请直接登录！',message:  i18n.t('DEFAULT.MSGACCOUNTEXIT') },
  {'ErrorCode': 605, label: 'Password与Confirm password不一致！',message:  i18n.t('DEFAULT.MSGOSWDIFF') },
  {'ErrorCode': 606, label: '注册失败！请检查网络是否正常',message: 'Registration failed! Please check whether the network is normal' }, 
  {'ErrorCode': 607, label: '已发送邮件到邮箱，请查收并确认',message: 'Email has been sent to mailbox, please check and confirm' }, 
  {'ErrorCode': 608, label: '邮箱不能为空！',message: 'Email cannot be empty!' }, 
  {'ErrorCode': 609, label: '非正确邮箱格式',message: 'Incorrect mailbox format' }, 
  {'ErrorCode': 610, label: '站点不能为空',message: 'Site cannot be empty' }, 
  {'ErrorCode': 611, label: '链接失效，请重新发送邮件',message: i18n.t('PERSONALCENTER.LINKFAILED') +'!'  }, 
  {'ErrorCode': 612, label: '邮箱已存在！',message: 'Email already exists!' }, 
  {'ErrorCode': 613, label: 'ID参数不能为空',message: 'ID parameter cannot be empty' }, 

  // {'ErrorCode': 614, label: '类型参数错误',message: '类型参数错误' }, 
  // {'ErrorCode': 615, label: '文章链接不能为空',message: '文章链接不能为空' }, 
  // {'ErrorCode': 616, label: '文章ID不能为空！',message: '文章ID不能为空！' }, 
  // {'ErrorCode': 617, label: '找不到该文章，请确认参数是否正确！',message: '找不到该文章，请确认参数是否正确！' }, 
  // {'ErrorCode': 618, label: '文章评论数更新失败',message: '文章评论数更新失败' }, 
  // {'ErrorCode': 619, label: '文章评论ID不能为空！',message: '文章评论ID不能为空！' }, 
  // {'ErrorCode': 620, label: '文章类型不能为空！',message: '文章类型不能为空！' }, 
  // {'ErrorCode': 621, label: '站点ID不能为空！',message: '站点ID不能为空！' }, 
  // {'ErrorCode': 622, label: '请传入一个分页参数',message: '请传入一个分页参数' }, 
  // {'ErrorCode': 623, label: '找不到文章详情页',message: '找不到文章详情页' }, 
  // {'ErrorCode': 624, label: '国家站点值不能为空',message: '国家站点值不能为空' }, 
  // {'ErrorCode': 625, label: '未查询到该订单',message: '未查询到该订单' }, 
  // {'ErrorCode': 626, label: '设备添加失败',message: '设备添加失败' }, 
  {'ErrorCode': 627, label: '邮箱已注册！',message: 'That email address is already registered. Please try another one.' }, 
]

const request = axios.create({
  baseURL:'/api/front/',
  transformRequest: [function (data) {
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }],
})
request.interceptors.request.use(config => {
  config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  return config
}, error => {
  Promise.reject(error)
})

request.interceptors.response.use(response =>{
  let state =  response.data.state == 0
  let status = response.data.status
  if(state){
    let isError = false
    for (let i = 0; i < msgList.length; i++) {
      const element = msgList[i];
      if(status == element.ErrorCode){
        isError = true
        Message.error(element.message)
      }
    }
    if(!isError){
      Message.error( response.data.message)
    }
  }

  if(response.status == 200){
    return response.data
  }else{
    Message.error('服务器端错误')
  }
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
})

export default request